package com.ethank.utils;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author tf
 * @date 2024-03-11
 * @Description 时间工具类
 */
public class DateCovertUtil {

    /**
    * @Description: 时间yyyy年MM月dd 转换成时间戳秒
    * @Param: [dateString]
    * @return: java.lang.Long
    * @Date: 2024-03-11
    */
    public static Long getTimeStamp(String dateString){

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy年M月d日");
        SimpleDateFormat formatYMD = new SimpleDateFormat("yyyy-MM-dd");

        try {
            Date date ;
            if(dateString.indexOf("年") !=-1){
                date = formatter.parse(dateString);
            }else if(dateString.indexOf("-") !=-1){
                date = formatYMD.parse(dateString);
            }else{
                date = new Date();
            }
            // 获取Date对象对应的毫秒时间戳
            return date.getTime()/1000;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("时间转换错误"+e.getMessage());
        }
        return 0L;
    }
    /**
     * @Description: 时间yyyy-MM-dd 转换成时间戳秒
     * @Param: [dateString]
     * @return: java.lang.Long
     * @Date: 2024-03-11
     */
    public static Long getSeconds(String dateString){

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");

        try {
            Date date = formatter.parse(dateString);
            // 获取Date对象对应的毫秒时间戳
            return date.getTime()/1000;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("时间转换错误"+e.getMessage());
        }
        return 0L;
    }
    /**
    * @Description: 获取当前时间 yyyy-MM-dd HH:mm:ss
    * @Param: []
    * @return: java.lang.String
    * @Author: tf
    * @Date: 2024-03-12
    */
    public  static  String getCurrentTime(){
        // 格式化日期和时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String formattedDateTime = sdf.format(new Date());
        return formattedDateTime;
    }

    /**
    * @Description: 时间戳转年-月-日
    * @Param: [timestampSeconds]
    * @return: java.lang.String
    * @Date: 2024-03-14
    */
    public  static  String secondsToFormat(long timestampSeconds,String format){

        SimpleDateFormat sdf = new SimpleDateFormat(format);
        String formattedDateTime = sdf.format(new Date(timestampSeconds*1000));

        return formattedDateTime;
    }

    /**
     * @Description: 时间yyyy年MM月dd 转换成时间戳秒
     * @Param: [dateString]
     * @return: java.lang.Long
     * @Date: 2024-03-11
     */
    public static Long getTimeStampV2(String dateString){

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy年M月d日");
        SimpleDateFormat formatYMD = new SimpleDateFormat("yyyy-MM-dd");

        try {
            Date date ;
            if(dateString.indexOf("日") !=-1){
                date = formatter.parse(dateString.substring(0,dateString.indexOf("日")+1));
            }else if(dateString.indexOf("-") !=-1){
                date = formatYMD.parse(dateString);
            }else{
                date = new Date();
            }
            // 获取Date对象对应的毫秒时间戳
            return date.getTime()/1000;
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("时间转换错误"+e.getMessage());
        }
        return 0L;
    }
}
